/*
 * @lc app=leetcode.cn id=748 lang=javascript
 *
 * [748] 最短补全词
 */

// @lc code=start
/**
 * @param {string} licensePlate
 * @param {string[]} words
 * @return {string}
 */
const inc = (arr1, arr2) => {
  let i1 = 0, i2 = 0;
  while (i1 < arr1.length && i2 < arr2.length) {
    if (arr1[i1] < arr2[i2]) {
      return false;
    }
    if (arr1[i1] === arr2[i2]) {
      i1++;
      i2++;
    } else {
      i2++;
    }
  }
  return i1 === arr1.length;
}
var shortestCompletingWord = function (licensePlate, words) {
  const l = licensePlate.toLowerCase().split('').filter(item => {
    return item >= 'a' && item <= 'z' || item >= 'a' && item <= 'z'
  }).sort();
  let result;

  words.forEach(word => {
    let d = word.split('').sort();
    if (inc(l, d)) {
      if (!result || word.length < result.length) {
        result = word;
      }
    }
  })
  return result;
};
// @lc code=end

